﻿'use strict';


// Declare app level module which depends on filters, and services
var app = angular.module('app', [
        'ionic',
        'app.filters',
        'app.services',
        'app.directives',
        'app.controllers',
        'base64',
        'ngStorage',
        'ui.load',
        'lazy-routing'
    ])
    .run(
        [
            '$rootScope', '$state', '$stateParams',
            function($rootScope, $state, $stateParams) {
                $rootScope.$state = $state;
                $rootScope.$stateParams = $stateParams;
            }
        ]
    )
    .constant('APP_CONFIG', {
        tokenCookieKey:     '_access_token',
        contack_cookie:     '_contack_cookie',
        openIdHeaderName:   'X-ACCESS_TOKEN',

        apiHost:            'http://localhost:9060/open',
        apiPrefix:          '/api',                                  //api 请求的前缀

        eventUserNotLogin:  'eventUserNotLogin',
        eventUserLoaded:    'eventUserLoaded',
        eventHttpError:     'eventHttpError',
        
        routeConfigUrl:     '/route.json'                           //动态配置state的数据
    })
    .config(
        [
                    '$stateProvider', '$urlRouterProvider', '$controllerProvider', '$compileProvider', '$filterProvider', '$provide', '$httpProvider', '$sceDelegateProvider', '$ionicConfigProvider','lazyRouterProvider', 'APP_CONFIG',
            function ($stateProvider, $urlRouterProvider, $controllerProvider, $compileProvider, $filterProvider, $provide, $httpProvider, $sceDelegateProvider, $ionicConfigProvider, lazyRouterProvider,APP_CONFIG) {

                // lazy controller, directive and service
                app.controller = $controllerProvider.register;
                app.directive = $compileProvider.directive;
                app.filter = $filterProvider.register;
                app.factory = $provide.factory;
                app.service = $provide.service;
                app.constant = $provide.constant;
                app.value = $provide.value;

                //ionic 				
                $ionicConfigProvider.platform.ios.tabs.style('standard');
                $ionicConfigProvider.platform.ios.tabs.position('bottom');                
                $ionicConfigProvider.platform.android.tabs.style('standard');
                $ionicConfigProvider.platform.android.tabs.position('bottom');
                $ionicConfigProvider.platform.ios.navBar.alignTitle('center');
                $ionicConfigProvider.platform.android.navBar.alignTitle('center');
                $ionicConfigProvider.platform.ios.backButton.previousTitleText('').icon('ion-ios-arrow-thin-left');
                $ionicConfigProvider.platform.android.backButton.previousTitleText('').icon('ion-android-arrow-back');
                $ionicConfigProvider.platform.ios.views.transition('ios');
                $ionicConfigProvider.platform.android.views.transition('android');

                //http interceptor
                $httpProvider.interceptors.push('appInterceptor');

                //white list for cross-domain
                $sceDelegateProvider.resourceUrlWhitelist([
                    'self',
                    APP_CONFIG.apiHost + '/**'
                ]);

                $urlRouterProvider
                    .when('/access/login', '/access/login/')
                   .otherwise('/app/home');

                //动态配置state的数据
                lazyRouterProvider.setCollectionUrl(APP_CONFIG.routeConfigUrl);
                           
                $stateProvider
                    .state('app', {
                        abstract: true,
                        url: '/app',
                        templateUrl: '/tpl/app.html',
                        resolve: {
                            tokenRes: [
                                'oauthService', function (oauthService) {
                                    return oauthService.loadTokenAsync();
                                }
                            ],
                            userRes: [
                                'accountService', 'tokenRes', function (accountService, tokenRes) {
                                    return accountService.getUserDataAsync();
                                }
                            ]
                        }
                    })
                    .state('app.home', {
                        url: '/home',
                        cache: false, //ionic 特有的
                        views: {
                            'home-tab': {
                                templateUrl: '/tpl/app-home.html',
                                resolve: {
                                    js:function($q) {
                                        var defer = $q.defer();
                                        defer.resolve(true);
                                        return defer.promise;
                                    }
                                }
                            }
                        }
                    })
                    .state('app.user', {
                        url: '/user',
                        views: {
                            'user-tab': {
                                templateUrl: '/tpl/app-user.html'
                            }
                        }
                    })
                    .state('app.settings', {
                        url: '/settings',
                        views: {
                            'home-tab': {
                                templateUrl: '/tpl/app-settings.html'
                            }
                        }
                    })
                    .state('app.contact', {
                        url: '/contact',
                        views: {
                            'home-tab': {
                                templateUrl: '/tpl/app-contact.html'
                            }
                        }
                    })
                    .state('access', {
                        url: '/access',
                        abstract: true,
                        template: '<div ui-view></div>',
                        resolve: {
                            tokenRes: [
                                'oauthService', function(oauthService) {
                                    return oauthService.loadTokenAsync();
                                }
                            ]
                        }
                    })
                    .state('access.login', {
                        url: '/login/:openId',
                        templateUrl: '/tpl/access-login.html'
                    })
                    ;
            }
        ]
    );
